﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BloodBank
{
    public partial class confirm : System.Web.UI.Page
    {
        //function to assign the values of examined donor data
        bool check_state() {

            string up = "update DONATIONPROCESS set HEMOGLOBINRATIO='" + h_rat.Text + "', PRESSURE ='" +  p_ree.Text + "', NUMDEROFBEAT ='" + n_n.Text + "' , TOPPLINGOFBEAT='" +  nabd2.Text + "',WEIGHT='" +weight.Text   + "',LENGTH='" + hieght.Text + "'  where BARCODE='" + Convert.ToInt32(Session["process_barcode"])+ "'";
            MainClass.RunDML(up);//update fieldes in database
           
                return true;

    
        }
        //to generate final result state
        bool check_diseases() {
            if (Session["diseases"] != null)
            {
                return true;//true donor have disease
            }
            else {

                return false;//dont have any disease
            }
        
        }
        bool check_sexrelatin() {
            if ((Session["sex_relation"] != null) && (Session["sex_relation"].ToString()=="yes"))
            {
                return true;//make relation
            }
           
            else { 
                return false; 
            }
 
        }
        bool check_alcohol() {
           // string a=(string)Session["cohol"];
            if ((Session["cohol"] != null) && (Session["cohol"].ToString()=="yes"))
            
                return true;//take alcohol
            
           
            
            else  //if session is null
               return false;
            
        }
        //to generate final result state
        bool generate_result() {
            if ((check_alcohol() == true) || (check_sexrelatin() == true))
            {
                return true;//donor is refused
            }
            else {
                return false;//donor is accepted
            }
        
        }
        void close_text() {
            h_rat.ReadOnly = true;
            p_ree.ReadOnly = true;
            n_n.ReadOnly = true;
            nabd2.ReadOnly = true;
            hieght.ReadOnly = true;
            weight.ReadOnly = true;
            //return 1;
        }
        bool check_hemoglopen(int h) {
           
              //int rate=  Convert.ToInt32(h_rat.Text);
                if (Session["gender"].ToString() == "male")
                {
                    if (13 < h && h < 17)
                    {
                        return true;//male accepted
                    }
                    else { return false; }
                }
                else if (Session["gender"].ToString() == "female")
                {
                    if (11 < h && h < 16)
                    {
                        return true;//female accepted
                    }
                    else { return false; }

                }
                else { return false; }
               
        }

        void show_final_boxes() {
            Label4.Visible = true;
            
            f_state.Visible = true;
            Label7.Visible = true;
            d_sig.Visible = true;
            Label8.Visible = true;
            d_name.Visible = true;
        }

      bool final_update() {
          string f_update = "update DONATIONPROCESS set COMPETENTSIGNATURE='" + d_name.Text + "', DOCTORSIGNATURE ='" + d_sig.Text + "', STATUS ='" + f_state.Text + "'  where BARCODE='" + Convert.ToInt32(Session["process_barcode"]) + "'";
          MainClass.RunDML(f_update);
          
          return true;
      
      
      }
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if(Session["process_barcode"]!=null){
                string a = Session["process_barcode"].ToString();
                l1.Text = a;
               
                }
            }
        }
        

        protected void f_check_Click(object sender, EventArgs e)
        {

            close_text();
            check_state();
         

           if (generate_result() == false && check_hemoglopen(Convert.ToInt32(h_rat.Text))==true)
            {//false
                state.Visible = true;
                state.Text = "تم قبول هذا المتبرع";
                reason.Visible = true;
                reason.Text = "   مستوفى الشروط السابقه ونسبه الهيموجلوبين  مناسبه";
                show_final_boxes();
                d_name.Text = Session["name"].ToString();
                f_state.Text = "accepted and hemoglopen ok";

            }
           else if ((generate_result() == false) && (check_hemoglopen(Convert.ToInt32(h_rat.Text)) == false))
            {

                state.Visible = true;
                state.Text = "تم رفض هذا المتبرع";
                reason.Visible = true;
                reason.Text = "   مستوفى الشروط السابقه ونسبه الهيموجلوبين غير مناسبه";
                show_final_boxes();
                d_name.Text = Session["name"].ToString();
                f_state.Text = "accepted and hemoglopen is not suitable";
            
            }
           else if ((generate_result() == true) && (check_hemoglopen(Convert.ToInt32(h_rat.Text)) == false))
           {

               state.Visible = true;
               state.Text = "تم رفض هذا المتبرع";
               reason.Visible = true;
               reason.Text = "   غير مستوفى الشروط السابقه ونسبه الهيمجلوبين غير مناسبه";
               show_final_boxes();
               d_name.Text = Session["name"].ToString();
               f_state.Text = "refused and hemoglopen isn t suitable";

           }
           else if ((generate_result() == true) && (check_hemoglopen(Convert.ToInt32(h_rat.Text)) == true))
           {

               state.Visible = true;
               state.Text = "تم رفض هذا المتبرع";
               reason.Visible = true;
               reason.Text = "   غير مستوفى الشروط السابقه ونسبه الهيمجلوبين  مناسبه";
               show_final_boxes();
               d_name.Text = Session["name"].ToString();
               f_state.Text = "refused and hemoglopen is suitable";
           }
           
            f_check.Visible = false;
            Session.Remove("diseases");
            Session.Remove("cohol");
            Session.Remove("sex_relation");
            Session.Remove("gender");
        }

        protected void finish_Click(object sender, EventArgs e)
        {
            final_update();
            Response.Redirect("~/Rception.aspx");
        }

      
    }
}